package com.cooba.activitys.user;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.activitys.login.LoginActivity;
import com.cooba.utils.Md5Utils;
import com.cooba.utils.MySqlOpenHelper;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Created by Administrator on 2015/12/16.
 * 修改密码
 */
public class ChangPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton mBackbtn;
    private EditText mEt_originalPassword;
    private EditText mNew_Password;
    private EditText mAgainPassword;
    private Button mSndButton;
    private String login_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_number);

        PushAgent.getInstance(this).onAppStart();
        mBackbtn = (ImageButton) findViewById(R.id.update_pwd_image_back);
        mEt_originalPassword = (EditText) findViewById(R.id.et_originalPassword);
        mNew_Password = (EditText) findViewById(R.id.new_Password);
        mAgainPassword = (EditText) findViewById(R.id.againPassword);
        mSndButton = (Button) findViewById(R.id.sendButton);
        mSndButton.setOnClickListener(this);
        mBackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.sendButton:

                if (TextUtils.isEmpty(mEt_originalPassword.getText()) || TextUtils.isEmpty(mNew_Password.getText()) || TextUtils.isEmpty(mAgainPassword.getText())) {
                    Toast.makeText(ChangPasswordActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
                if (!mNew_Password.getText().toString().trim().matches(regex)) {
                    Toast.makeText(ChangPasswordActivity.this, "密码格式不正确!", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    login_id = SharedPreferencesUtil.getData(ChangPasswordActivity.this, "login_id", "");
                    String originalPassword = Md5Utils.encodeBy32BitMD5(mEt_originalPassword.getText().toString());
                    String New_Password = Md5Utils.encodeBy32BitMD5(mNew_Password.getText().toString());
                    String AgainPassword = Md5Utils.encodeBy32BitMD5(mAgainPassword.getText().toString());
                    if (New_Password.equals(AgainPassword)) {
                        HttpUtils httpUtils = new HttpUtils();
                        RequestParams params = new RequestParams();
                        params.addBodyParameter("login_id", login_id);
                        params.addBodyParameter("old_password", originalPassword);
                        params.addBodyParameter("new_password", New_Password);
                        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.ChangePwd, params, new RequestCallBack<String>() {
                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                Log.e("wei", "修改失败");
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                String msg = null;
                                try {
                                    msg = JSON.parseObject(responseInfo.result).getString("msg");
                                    String status = JSON.parseObject(responseInfo.result).getString("status");
                                    if (status.equals("0")) {
                                        Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(getApplication(), "密码修改成功", Toast.LENGTH_SHORT).show();
                                        SharedPreferencesUtil.saveData(ChangPasswordActivity.this, "login_status", "");
                                        SharedPreferencesUtil.saveData(ChangPasswordActivity.this, "login_id", "");
                                        SharedPreferencesUtil.saveData(ChangPasswordActivity.this, "login_name", "");
                                        SharedPreferencesUtil.saveData(ChangPasswordActivity.this, "login_integral", "");
                                        Intent intent = new Intent(ChangPasswordActivity.this, LoginActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    } else {
                        Toast.makeText(getApplication(), "两次输入密码不一样", Toast.LENGTH_SHORT).show();
                    }

                }
                break;

        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
